<?php 
class ControllerNewsAuthor extends Controller {  
	public function index() { 
		$this->language->load('news/author');
		
		$this->load->model('news/author');
		$this->load->model('news/utf8');
		
		$this->load->model('tool/image');		
		
		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_related'] = $this->language->get('text_related');	
		
		$this->data['text_index'] = $this->language->get('text_index');
		$this->data['text_login'] = $this->language->get('text_login');
		$this->data['text_register_author'] = $this->language->get('text_register_author');
		$this->data['register_author'] = $this->url->link('news/register');
		$this->data['login'] = $this->url->link('news/login');
		$this->data['text_article'] = $this->language->get('text_article');
		$this->data['text_empty'] = $this->language->get('text_empty');
		
		$this->data['button_continue'] = $this->language->get('button_continue');
		
		$this->data['breadcrumbs'] = array();
		
      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),
        	'separator' => false
      	);
		
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_author'),
			'href'      => $this->url->link('news/author'),
			'separator' => $this->language->get('text_separator')
		);
		
		$this->load->model('news/article');
		$this->load->model('news/utf8');
		$this->data['categories'] = array();
									
		$results = $this->model_news_author->getAuthors();
		foreach ($results as $result) {
			if (is_numeric($this->model_news_utf8->utf8_substr($result['author'], 0, 1))) {
				$key = '0 - 9';
			} else {
				$key = $this->model_news_utf8->utf8_substr($this->model_news_utf8->utf8_strtoupper($result['author']), 0, 1);
			}
			
			if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
				$image = $this->model_tool_image->resize($result['image'], 80, 80);
			} else {
				$image = $this->model_tool_image->resize('no_avatar.jpg', 80, 80);
			}
			if (!isset($this->data['authors'][$key])) {
				$this->data['categories'][$key]['name'] = $key;
			}
			
			$total = $this->model_news_article->getTotalArticlesByAuthorId($result['author_id']);
			$this->data['categories'][$key]['author'][] = array(
				'author' => $result['author'],
				'total' => $total,
				'image' => $image,
				'href' => $this->url->link('news/author/info', 'author_id=' . $result['author_id'])
			);
		}
		
		$this->data['continue'] = $this->url->link('common/home');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/author_list.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/news/author_list.tpl';
		} else {
			$this->template = 'default/template/news/author_list.tpl';
		}			
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'
		);
				
		$this->response->setOutput($this->render());										
  	}
	
	public function info() {
    	$this->language->load('news/author');
		
		$this->load->model('news/author');
		
		$this->load->model('news/article');
		
		$this->load->model('tool/image'); 
		
		if (isset($this->request->get['author_id'])) {
			$author_id = (int)$this->request->get['author_id'];
		} else {
			$author_id = 0;
		} 										
		
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = $this->config->get('config_article_sort');
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = $this->config->get('config_article_order');
		}
  		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
				
		if (isset($this->request->get['limit'])) {
			$limit = $this->request->get['limit'];
		} else {
			$limit = $this->config->get('config_catalog_limit');
		}

		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array( 
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),
      		'separator' => false
   		);
   		
		$this->data['breadcrumbs'][] = array( 
       		'text'      => $this->language->get('text_author'),
			'href'      => $this->url->link('news/author'),
      		'separator' => $this->language->get('text_separator')
   		);
		
		$author_info = $this->model_news_author->getAuthor($author_id);
	
		if ($author_info) {
			$this->document->setTitle($author_info['author']);
			
			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	
	
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
					
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}	
			
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
		   			
			$this->data['breadcrumbs'][] = array(
       			'text'      => $author_info['author'],
				'href'      => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . $url),
      			'separator' => $this->language->get('text_separator')
   			);
		
			$this->data['heading_title'] = $author_info['author'];
					
			if ($author_info['image']) {
				$this->data['thumb'] = $this->model_tool_image->resize($author_info['image'], $this->config->get('config_article_category_width'), $this->config->get('config_article_category_height'));
			} else {
				$this->data['thumb'] = '';
			}
							
			$this->data['author_description'] = html_entity_decode($author_info['author_description'], ENT_QUOTES, 'UTF-8');
			
			$this->data['text_empty'] = $this->language->get('text_empty');
			$this->data['text_date_added'] = $this->language->get('text_date_added');
			$this->data['text_by_author'] = $this->language->get('text_by_author');
			$this->data['text_more'] = $this->language->get('text_more');
			$this->data['text_display'] = $this->language->get('text_display');
			$this->data['text_list'] = $this->language->get('text_list');
			$this->data['text_grid'] = $this->language->get('text_grid');			
			$this->data['text_sort'] = $this->language->get('text_sort');
			$this->data['text_limit'] = $this->language->get('text_limit');
			$this->data['text_related'] = $this->language->get('text_related');
			$this->data['text_author'] = $this->language->get('text_author');
			   
			$this->data['button_cart'] = $this->language->get('button_cart');
			$this->data['button_wishlist'] = $this->language->get('button_wishlist');
			$this->data['button_compare'] = $this->language->get('button_compare');
			$this->data['button_continue'] = $this->language->get('button_continue');
			$this->data['width'] = $this->config->get('config_image_article_width');
			
			$this->data['compare'] = $this->url->link('news/compare');
			
			$this->data['articles'] = array();
			
			$data = array(
				'filter_author_id' => $author_id, 
				'sort'                   => $sort,
				'order'                  => $order,
				'start'                  => ($page - 1) * $limit,
				'limit'                  => $limit
			);
					
			$article_total = $this->model_news_article->getTotalArticles($data); 
			
			$results = $this->model_news_article->getArticles($data);
			$this->load->model('news/utf8');
			if($this->config->get('article_description_limit')){
				$description_limit=$this->config->get('article_description_limit');
			}			else{
				$description_limit=255;
			}
			foreach ($results as $result) {	
					$related_articles = array();
						
					$related = $this->model_news_article->getArticleRelatedLimit($result['article_id'],3);	
					foreach ($related as $relate) {							
						$related_articles[] = array(
							'article_id' => $relate['article_id'],
							'name'    	 => $relate['name'],
							'href'    	 => $this->url->link('news/article', 'article_id=' . $relate['article_id']),
						);
					}	
				if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_article_width'), $this->config->get('config_image_article_height'));
				} else {
					$image = false;
				}		
				
				if ($this->config->get('config_comment_status')) {
					$rating = (int)$result['rating'];
				} else {
					$rating = false;
				}
								
				$this->data['articles'][] = array(
					'article_id'  => $result['article_id'],
					'related' => $related_articles,
					'thumb'       => $image,
					'name'        => $result['name'],
					'date_added' => date($this->config->get('config_article_date_format'), strtotime($result['date_added'])),
					'author'        => $result['author'],
					'author_href'    	 => $this->url->link('news/author', 'author_id=' . $result['author_id']),
					'description' => $this->model_news_utf8->utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $description_limit) . '..',
					'rating'      => $result['rating'],
					'viewed'     => sprintf($this->language->get('text_viewed'), (int)$result['viewed']),
					'comments'     => sprintf($this->language->get('text_comments'), (int)$result['comments']),
					'href'        => $this->url->link('news/article','author_id='.$author_id.'&article_id=' . $result['article_id']. $url )
				);
			}
					
			$url = '';
			
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
						
			$this->data['sorts'] = array();
			
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_date_desc'),
				'value' => 'p.date_added-DESC',
				'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . '&sort=p.date_added&order=DESC' . $url)
			);
			
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_name_asc'),
				'value' => 'pd.name-ASC',
				'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . '&sort=pd.name&order=ASC' . $url)
			); 
			
	
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_name_desc'),
				'value' => 'pd.name-DESC',
				'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . '&sort=pd.name&order=DESC' . $url)
			);
			
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_sort_asc'),
				'value' => 'p.sort_order-ASC',
				'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . '&sort=p.sort_order&order=ASC' . $url)
			);

			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_sort_desc'),
				'value' => 'p.sort_order-DESC',
				'href'  => $this->url->link('news/author/info','author_id=' . $this->request->get['author_id'] . '&sort=p.sort_order&order=DESC' . $url)
			);
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_viewed_asc'),
				'value' => 'p.viewed-ASC',
				'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . '&sort=p.viewed&order=ASC' . $url)
			);

			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_viewed_desc'),
				'value' => 'p.viewed-DESC',
				'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . '&sort=p.viewed&order=DESC' . $url)
			);
	
			if ($this->config->get('config_comment_status')) {
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_rating_desc'),
					'value' => 'rating-DESC',
					'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . '&sort=rating&order=DESC' . $url)
				); 
				
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_rating_asc'),
					'value' => 'rating-ASC',
					'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . '&sort=rating&order=ASC' . $url)
				);
			}
			
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_author_asc'),
				'value' => 'na.author-ASC',
				'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . '&sort=na.author&order=ASC' . $url)
			); 
	
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_author_desc'),
				'value' => 'na.author-DESC',
				'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . '&sort=na.author&order=DESC' . $url)
			);
	
			$url = '';
					
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	
	
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->data['limits'] = array();
			
			$this->data['limits'][] = array(
				'text'  => $this->config->get('config_catalog_limit'),
				'value' => $this->config->get('config_catalog_limit'),
				'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . $url . '&limit=' . $this->config->get('config_catalog_limit'))
			);
						
			$this->data['limits'][] = array(
				'text'  => 25,
				'value' => 25,
				'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . $url . '&limit=25')
			);
			
			$this->data['limits'][] = array(
				'text'  => 50,
				'value' => 50,
				'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . $url . '&limit=50')
			);
	
			$this->data['limits'][] = array(
				'text'  => 75,
				'value' => 75,
				'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . $url . '&limit=75')
			);
			
			$this->data['limits'][] = array(
				'text'  => 100,
				'value' => 100,
				'href'  => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id'] . $url . '&limit=100')
			);
					
			$url = '';
							
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	
	
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
					
			$pagination = new Pagination();
			$pagination->total = $article_total;
			$pagination->page = $page;
			$pagination->limit = $limit;
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = $this->url->link('news/author/info','author_id=' . $this->request->get['author_id'] .  $url . '&page={page}');
			
			$this->data['pagination'] = $pagination->render();
			
		$this->data['config_sort'] = $this->config->get('config_article_sort');
			$this->data['sort'] = $sort;
			$this->data['order'] = $order;
			$this->data['limit'] = $limit;
			
			$this->data['continue'] = $this->url->link('common/home');
			
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/author_info.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/news/author_info.tpl';
			} else {
				$this->template = 'default/template/news/author_info.tpl';
			}
			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);
					
			$this->response->setOutput($this->render());
		} else {
			$url = '';
			
			if (isset($this->request->get['author_id'])) {
				$url .= '&author_id=' . $this->request->get['author_id'];
			}
									
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
				
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
						
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
						
			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_error'),
				'href'      => $this->url->link('news/category', $url),
				'separator' => $this->language->get('text_separator')
			);
				
			$this->document->setTitle($this->language->get('text_error'));

      		$this->data['heading_title'] = $this->language->get('text_error');

      		$this->data['text_error'] = $this->language->get('text_error');

      		$this->data['button_continue'] = $this->language->get('button_continue');

      		$this->data['continue'] = $this->url->link('common/home');

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}
			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);
					
			$this->response->setOutput($this->render());
		}
  	}
}
?>